/**
 * Created by koudaiwang on 2017/7/20.
 */


var TradimgPlatformModel = require('../Dao/tradingplatformdao');
var JsonModel = require('../config/jsonModel');
var CheckLogin = require('./checkLogin').checkAdminLogin;


var fs = require('fs');
var uploadplatformimage = require('./uploadImage').uploadplatformimage;

module.exports = function(app) {

    app.post('/addplatform',function (req, res) {
        uploadplatformimage(req, res, function (path, fields) {
            console.log(fields);
            var model = new JsonModel.JsonModel();
            var name            = fields.name[0];
            var platformurl     = fields.platformurl[0];
            var tradingVolume   = Number(fields.tradingVolume[0]) || -1;
            var foundDate       = fields.foundDate[0];
            var des             = fields.des[0];
            var level           = Number(fields.level[0]) || -1;

            model.status = 0;

            try {
                if (!(name.length >= 1 && name.length < 100)) {
                    throw new Error('平台名称在1-100字!');
                }
                if (!(platformurl.length >= 1 && platformurl.length < 10000000)) {
                    throw new Error('平台官网不能为空!');
                }

                if (tradingVolume === -1) {
                    throw new Error('平台交易量不能为空!');
                }

                if (!foundDate) {
                    throw new Error('平台创建日期不能为空!');
                }

                if (!des) {
                    throw new Error('平台介绍不能为空!');
                }

                if (level === -1) {
                    throw new Error('平台等级不能为空!');
                }


            } catch (e) {
                fs.unlinkSync(path); //删除文件
                model.content = e.message;
                model.msg = e.message;
                return res.send(model);
            }

            var platform = {
                name: name,
                platformurl: platformurl,
                tradingVolume: tradingVolume,
                foundDate: foundDate,
                des: des,
                level: level,
                iconimage: path.replace('../client', ''),
            };
            TradimgPlatformModel.create(platform)
                .then(function (result) {
                    console.log(result.ops[0]);
                    console.log('插入成功!');

                    var model = new JsonModel.JsonModel();
                    model.msg = '成功';
                    model.content = '插入成功!';
                    res.send(model);

                }).catch(function(err){
                fs.unlinkSync(path); //删除文件
                model.msg = JSON.stringify(err);
                model.content = "操作失败";
                res.send(model);
            });
        });
    });

    // ----------------- 获取所有平台信息
    app.get('/getAllPlatformInfo', function (req, res) {
        TradimgPlatformModel.getAllPlatform().then(function (platformList) {
            var model = new JsonModel.JsonModel();
            model.msg = '成功';
            model.content = {platformList: platformList};
            return res.send(model);
        });
    });


    app.get('/getPlatformByID', function (req, res) {
        var model = new JsonModel.JsonModel();
        model.status = 0;
        var ID = req.query.id || -1;
        try {
            if (ID === -1) {
                throw new Error('id不能为空!');
            }
        } catch (e) {
            model.content = e.message;
            model.msg = e.message;
            return res.send(model);
        }

        TradimgPlatformModel.getplatformID(ID).then(function (platformInfo) {
            var model = new JsonModel.JsonModel();
            var platformInfos = platformInfo[0];
            if (!platformInfos) {
                model.msg = '没有这个ID';
                model.content = {};
            } else {
                model.msg = '成功';
                model.content = platformInfos;
                model.status = 1;
            }
            res.send(model);
        })
    });


    app.get('/getPlatformByName', function (req, res) {
        var model = new JsonModel.JsonModel();
        model.status = 0;
        var name = req.query.name;
        try {
            if (!name) {
                throw new Error('id不能为空!');
            }
        } catch (e) {
            model.content = e.message;
            model.msg = e.message;
            return res.send(model);
        }

        TradimgPlatformModel.getplatformName(name).then(function (platformInfo) {
            var model = new JsonModel.JsonModel();
            var platformInfos = platformInfo[0];
            if (!platformInfos) {
                model.msg = '没有这个平台';
                model.content = {};
            } else {
                model.msg = '成功';
                model.content = platformInfos;
                model.status = 1;
            }
            res.send(model);
        })
    });



}